package com.panghl.controller;

import com.panghl.model.vo.SysFileVO;
import com.panghl.service.ISysAttachService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author panghl
 * @Date 2021/9/22 14:41
 * @Description 附件-管理
 **/
@RestController
@RequestMapping("sysAttach")
public class SysAttachController {


    @Value(value = "${system.native.path}")
    private String systemPath;


    @Autowired
    private ISysAttachService sysAttachService;

    /**
     * 附件上传
     * @param file -
     * @return
     */
    @ApiOperation(value = "上传附件", notes = "上传附件")
    @PostMapping(value = "/upload")
    public SysFileVO upload(@RequestPart("file") MultipartFile file) {
        return sysAttachService.upload(file);
    }

    /**
     * 附件预览
     * @param filePath  文件地址
     * @param response  返回流
     */
    @ApiOperation(value = "附件预览", notes = "附件预览")
    @GetMapping(value = "/preview")
    public void preview(String filePath,String type, HttpServletResponse response) {
        sysAttachService.preview(filePath,type,response);
        return ;
    }

    /**
     * 附件预览2
     * @param filePath  文件地址
     * @param response  返回流
     */
    @ApiOperation(value = "附件预览-本地方式", notes = "附件预览-本地方式")
    @GetMapping(value = "/preview2")
    public void preview2(String filePath, String type, HttpServletResponse response, HttpServletRequest request) {
        sysAttachService.preview2(systemPath+filePath,type,response,request);
        return ;
    }

    /**
     * 附件下载
     * @param filePath  文件地址
     * @param response  返回流
     */
    @ApiOperation(value = "附件下载", notes = "附件下载")
    @GetMapping(value = "/download")
    public void download(String filePath, HttpServletResponse response) {
        sysAttachService.download(filePath,response);
        return;
    }


}
